package view.drawable;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Iterator;

/**
 * Created by rovayazt on 21/02/14.
 */
public class LineIterator implements Iterator<GenerateGraphicalArc.PointF> {
    final static double DEFAULT_PRECISION = (float) 1.0;
    final Line2D line;
    final double precision;

    final double sx, sy;
    final double dx, dy;

    float x,y;
    double error;

    public LineIterator(Line2D line, double precision) {
        this.line = line;
        this.precision = precision;

        sx = line.getX1() < line.getX2() ? precision : -1 * precision;
        sy = line.getY1() < line.getY2() ? precision : -1 * precision;

        dx = (float) Math.abs(line.getX2() - line.getX1());
        dy = (float) Math.abs(line.getY2() - line.getY1());

        error = dx - dy;

        y = (float) line.getY1();
        x = (float) line.getX1();
    }

    public LineIterator(Line2D line) {
        this(line, DEFAULT_PRECISION);
    }

    @Override
    public boolean hasNext() {
        return Math.abs( x - line.getX2()) > 0.9 || ( Math.abs(y - line.getY2()) > 0.9);
    }

    @Override
    public GenerateGraphicalArc.PointF next() {
        GenerateGraphicalArc.PointF ret = new GenerateGraphicalArc.PointF(x, y);

        double e2 = 2*error;
        if (e2 > -dy) {
            error -= dy;
            x += sx;
        }
        if (e2 < dx) {
            error += dx;
            y += sy;
        }

        return ret;
    }

    @Override
    public void remove() {
        throw new AssertionError();
    }
}
